package com.gpframework.common.core.domain.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.gpframework.common.annotation.Excel;
import com.gpframework.common.annotation.Excel.ColumnType;
import com.gpframework.common.annotation.Excel.Type;
import com.gpframework.common.annotation.Excels;
import com.gpframework.common.constant.BrandConstants;
import com.gpframework.common.core.domain.BaseEntity;
import io.swagger.annotations.ApiModelProperty;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.springframework.util.StringUtils;

import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import java.util.Date;
import java.util.List;

/**
 * 用户对象 sys_user
 *
 * @Author gpframework
 */
public class SysUser extends BaseEntity {
    private static final long serialVersionUID = 1L;

    /**
     * 用户ID
     */
    @Excel(name = "用户序号", cellType = ColumnType.NUMERIC, prompt = "用户编号")
    @JsonFormat(shape = JsonFormat.Shape.STRING)
    @ApiModelProperty("用户ID")
    private Long brandUserId;

    @ApiModelProperty("用户ID")
    private Long userId;

    /**
     * 品牌id
     */
    @Excel(name = "品牌id")
    @JsonFormat(shape = JsonFormat.Shape.STRING)
    @ApiModelProperty("品牌id")
    private Long brandId;

    @Excel(name = "门店id")
    @ApiModelProperty("门店id")
    @JsonFormat(shape = JsonFormat.Shape.STRING)
    private Long storeId;
    /**
     * 是否为管理员
     */
    @ApiModelProperty("是否为管理员")
    @Excel(name = "是否为管理员")
    private Integer isAdmin;

    /**
     * 是否为门店管理员
     */
    @ApiModelProperty("是否为门店管理员")
    @Excel(name = "是否为门店管理员")
    private Integer storeAdmin = 1;

    /**
     * 部门ID
     */
    @Excel(name = "部门编号")
    @ApiModelProperty("部门ID")
    @JsonFormat(shape = JsonFormat.Shape.STRING)
    private Long brandDeptId;


    private Long deptId;

    private String roleKey;

    /**
     * 用户账号
     */
    @ApiModelProperty("用户账号")
    @Excel(name = "登录名称")
    private String userName;

    /**
     * 用户昵称
     */
    @ApiModelProperty("用户昵称")
    @Excel(name = "用户名称")
    private String nickName;

    /**
     * 系统布局配置
     */
    @ApiModelProperty("系统布局配置")
    @Excel(name = "系统布局配置")
    private String configuration;

    /**
     * 用户邮箱
     */
    @ApiModelProperty("用户邮箱")
    @Excel(name = "用户邮箱")
    private String email;

    /**
     * 手机号码
     */
    @ApiModelProperty("手机号码")
    @Excel(name = "手机号码")
    private String phonenumber;

    /**
     * 用户性别
     */
    @ApiModelProperty("用户性别")
    @Excel(name = "用户性别", readConverterExp = "0=男,1=女,2=未知")
    private String sex;

    /**
     * 用户头像
     */
    @ApiModelProperty("用户头像")
    private String avatar;

    /**
     * 密码
     */
    @ApiModelProperty("密码")
    private String password;

    /**
     * 显示的密码
     */
    @ApiModelProperty("显示的密码")
    private String showPassword;

    /**
     * 盐加密
     */
    private String salt;

    /**
     * 帐号状态（0正常 1停用）
     */
    @Excel(name = "帐号状态", readConverterExp = "0=正常,1=停用")
    private String status;

    /** 绑定类型 0供应商，1客户，2清关公司，3国内代理 */
    private String bindingType;

    /** 绑定id */
    private Long bindingId;

    /** 是否绑定： 0否 1是 */
    private String bindingFlag;

    /** 绑定服务商id */
    private Long supplierId;

    /**
     * 删除标识
     */
    private Integer delStatus;

    /**
     * 最后登录IP
     */
    @Excel(name = "最后登录IP", type = Type.EXPORT)
    private String loginIp;

    /**
     * 最后登录时间
     */
    @Excel(name = "最后登录时间", width = 30, dateFormat = "yyyy-MM-dd HH:mm:ss", type = Type.EXPORT)
    private Date loginDate;

    /**
     * 部门对象
     */
    @Excels({
            @Excel(name = "部门名称", targetAttr = "deptName", type = Type.EXPORT),
            @Excel(name = "部门负责人", targetAttr = "leader", type = Type.EXPORT)
    })
    private SysDept dept;

    /**
     * 角色对象
     */
    @ApiModelProperty("角色对象")
    private List<SysRole> roles;

    /**
     * 角色组
     */
    @ApiModelProperty("角色组")
    private Long[] roleIds;

    /**
     * 岗位组
     */
    @ApiModelProperty("岗位组")
    private Long[] postIds;

    /**
     * 微信用户id
     */
    @ApiModelProperty("微信用户id")
    private Long wxUserId;

    public String getBindingType() {
        return bindingType;
    }

    public void setBindingType(String bindingType) {
        this.bindingType = bindingType;
    }

    public Long getBindingId() {
        return bindingId;
    }

    public void setBindingId(Long bindingId) {
        this.bindingId = bindingId;
    }

    public String getBindingFlag() {
        return bindingFlag;
    }

    public void setBindingFlag(String bindingFlag) {
        this.bindingFlag = bindingFlag;
    }

    /**
     * 微信用户名称
     */
    @ApiModelProperty("微信用户名称")
    private String wxUserName;

    @ApiModelProperty("登录token")
    private String loginToken;

    @ApiModelProperty("用户的工会Id集合")
    private String guildIds;

    /**
     * 品牌名称
     */
    @Excel(name = "品牌名称")
    @JsonFormat(shape = JsonFormat.Shape.STRING)
    @ApiModelProperty("品牌名称")
    private String brandName;

    @Excel(name = "用户头像")
    @JsonFormat(shape = JsonFormat.Shape.STRING)
    @ApiModelProperty("用户头像")
    private String headImage;

    @ApiModelProperty("关键词")
    private String keyWord;

    public String getKeyWord() {
        return keyWord;
    }

    public void setKeyWord(String keyWord) {
        this.keyWord = keyWord;
    }

    public Integer getStoreAdmin() {
        return storeAdmin;
    }

    public void setStoreAdmin(Integer storeAdmin) {
        this.storeAdmin = storeAdmin;
    }

    public String getHeadImage() {
        return headImage;
    }

    public void setHeadImage(String headImage) {
        this.headImage = headImage;
    }

    public String getBrandName() {
        return brandName;
    }

    public void setBrandName(String brandName) {
        this.brandName = brandName;
    }

    public String getWxUserName() {
        return wxUserName;
    }

    public void setWxUserName(String wxUserName) {
        this.wxUserName = wxUserName;
    }

    public Long getWxUserId() {
        return wxUserId;
    }

    public void setWxUserId(Long wxUserId) {
        this.wxUserId = wxUserId;
    }

    public Long getStoreId() {
        return storeId;
    }

    public void setStoreId(Long storeId) {
        this.storeId = storeId;
    }

    public SysUser() {

    }

    public SysUser(Long brandId, String mobile, String nickName, String brandLogo) {
        this.brandId = brandId;
        this.userName = mobile;
        this.nickName = nickName;
        this.phonenumber = mobile;
        this.avatar = brandLogo;
//        this.password = SecurityUtils.encryptPassword(mobile);
        this.password = BrandConstants.USER_PASSWORD;
        this.showPassword = BrandConstants.SHOW_USER_PASSWORD;
        this.status = "0";
    }

    public SysUser(Long userId) {
        this.brandUserId = userId;
    }

    public Long getBrandUserId() {
        return brandUserId;
    }

    public void setBrandUserId(Long brandUserId) {
        this.brandUserId = brandUserId;
    }

    public Long getBrandId() {
        return brandId;
    }

    public void setBrandId(Long brandId) {
        this.brandId = brandId;
    }

    public Integer getIsAdmin() {
        return isAdmin;
    }

    public Boolean whetherAdmin() {
        if (StringUtils.isEmpty(isAdmin) || isAdmin.equals(1)) {
            return false;
        }
        return true;
    }


    public Boolean isAdmIN() {
        if (com.gpframework.common.utils.StringUtils.isNotEmpty(this.roles)) {
            for (SysRole role : this.roles) {
                if ("admin".equals(role.getRoleKey())){
                    return true;
                }
            }
        }
        return false;
    }

    public Long getDeptId() {
        return deptId;
    }

    public void setDeptId(Long deptId) {
        this.deptId = deptId;
    }

    public Boolean whetherStoreAdmin() {
        if (StringUtils.isEmpty(storeAdmin) || storeAdmin.equals(1)) {
            return false;
        }
        return true;
    }

    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    public static boolean isAdmin(Long userId) {
        return userId != null && 1L == userId;
    }

    public boolean isAdmin() {
        return isAdmin(this.brandUserId);
    }

    public Long getBrandDeptId() {
        return brandDeptId;
    }

    public void setBrandDeptId(Long brandDeptId) {
        this.brandDeptId = brandDeptId;
    }

    @Size(min = 0, max = 30, message = "用户昵称长度不能超过30个字符")
    public String getNickName() {
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }

    @NotBlank(message = "用户账号不能为空")
    @Size(min = 0, max = 30, message = "用户账号长度不能超过30个字符")
    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    @Email(message = "邮箱格式不正确")
    @Size(min = 0, max = 50, message = "邮箱长度不能超过50个字符")
    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Size(min = 0, max = 11, message = "手机号码长度不能超过11个字符")
    public String getPhonenumber() {
        return phonenumber;
    }

    public void setPhonenumber(String phonenumber) {
        this.phonenumber = phonenumber;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getAvatar() {
        return avatar;
    }

    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }

    @JsonIgnore
    @JsonProperty
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getSalt() {
        return salt;
    }

    public void setSalt(String salt) {
        this.salt = salt;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }


    public Long getSupplierId() {
        return supplierId;
    }

    public void setSupplierId(Long supplierId) {
        this.supplierId = supplierId;
    }

    public Integer getDelStatus() {
        return delStatus;
    }

    public void setDelStatus(Integer delStatus) {
        this.delStatus = delStatus;
    }

    public String getLoginIp() {
        return loginIp;
    }

    public void setLoginIp(String loginIp) {
        this.loginIp = loginIp;
    }

    public Date getLoginDate() {
        return loginDate;
    }

    public String getRoleKey() {
        return roleKey;
    }

    public void setRoleKey(String roleKey) {
        this.roleKey = roleKey;
    }

    public void setLoginDate(Date loginDate) {
        this.loginDate = loginDate;
    }

    public SysDept getDept() {
        return dept;
    }

    public void setDept(SysDept dept) {
        this.dept = dept;
    }

    public List<SysRole> getRoles() {
        return roles;
    }

    public void setRoles(List<SysRole> roles) {
        this.roles = roles;
    }

    public Long[] getRoleIds() {
        return roleIds;
    }

    public void setRoleIds(Long[] roleIds) {
        this.roleIds = roleIds;
    }

    public Long[] getPostIds() {
        return postIds;
    }

    public void setPostIds(Long[] postIds) {
        this.postIds = postIds;
    }

    public void setIsAdmin(Integer isAdmin) {
        this.isAdmin = isAdmin;
    }

    public String getShowPassword() {
        return showPassword;
    }

    public void setShowPassword(String showPassword) {
        this.showPassword = showPassword;
    }

    public String getLoginToken() {
        return loginToken;
    }

    public void setLoginToken(String loginToken) {
        this.loginToken = loginToken;
    }

    public String getGuildIds() {
        return guildIds;
    }

    public void setGuildIds(String guildIds) {
        this.guildIds = guildIds;
    }

    public String getConfiguration() {
        return configuration;
    }

    public void setConfiguration(String configuration) {
        this.configuration = configuration;
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
                .append("brandUserId", getBrandUserId())
                .append("brandDeptId", getBrandDeptId())
                .append("storeId", getStoreId())
                .append("isAdmin", getIsAdmin())
                .append("userName", getUserName())
                .append("nickName", getNickName())
                .append("email", getEmail())
                .append("phonenumber", getPhonenumber())
                .append("sex", getSex())
                .append("avatar", getAvatar())
                .append("password", getPassword())
                .append("showPassword", getShowPassword())
                .append("salt", getSalt())
                .append("status", getStatus())
                .append("supplierId", getSupplierId())
                .append("bindingId", getBindingId())
                .append("bindingFlag", getBindingFlag())
                .append("bindingType", getBindingType())
                .append("delStatus", getDelStatus())
                .append("loginIp", getLoginIp())
                .append("loginDate", getLoginDate())
                .append("createBy", getCreateBy())
                .append("createTime", getCreateTime())
                .append("updateBy", getUpdateBy())
                .append("updateTime", getUpdateTime())
                .append("remark", getRemark())
                .append("dept", getDept())
                .append("wxUserId", getWxUserId())
                .append("loginToken", getLoginToken())
                .append("headImage", getHeadImage())
                .toString();
    }
}
